//
//  HNHomeMainHeaderView.m
//  HappyNews
//
//  Created by baiping on 15/12/28.
//  Copyright © 2015年 bai wang. All rights reserved.
//

#import "HNHomeMainHeaderView.h"
#define kmainScrollViewHeight 220


@implementation HNHomeMainHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kmainScrollViewHeight)];
        
        _mainScrollView.contentSize = CGSizeMake(kScreenWidth * 4, 0);
        
        _mainScrollView.bounces = YES;
        
        _mainScrollView.alwaysBounceHorizontal = YES;
        
        _mainScrollView.alwaysBounceVertical = NO;
        
        _mainScrollView.pagingEnabled = YES;
        
        [self addSubview:_mainScrollView];
        
        _fristScrollBtn = [self addScrollBtn:0];
        
        [_mainScrollView addSubview:_fristScrollBtn];
        
        _secondScrollBtn = [self addScrollBtn:1];
        
        [_mainScrollView addSubview:_secondScrollBtn];
        
        
        _thridScrollBtn = [self addScrollBtn:2];
        
        [_mainScrollView addSubview:_thridScrollBtn];
        
        _fourScrollBtn = [self addScrollBtn:3];
        
        [_mainScrollView addSubview:_fourScrollBtn];
        
        // 设置初始偏移量
        
        [_mainScrollView setContentOffset:CGPointMake(kScreenWidth, 0)];
        
        //  _thirdSelectionBtn.backgroundColor = [UIColor yellowColor];
        // 添加分页
        self.pageControl = [[UIPageControl alloc] initWithFrame:(CGRectMake(0, _fristScrollBtn.bottom - 60, kScreenWidth, 50))];
        
        self.pageControl.numberOfPages = 3;
        
        self.pageControl.currentPage = 0;
        
        self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        self.pageControl.pageIndicatorTintColor = [UIColor blackColor];
        
        self.mainScrollView.showsHorizontalScrollIndicator = NO;
        
        [self addSubview:self.pageControl];
        
    }
    
    return self;
    
}

- (UIButton *)addScrollBtn:(NSInteger)index{
    
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    btn.frame = CGRectMake(index * kScreenWidth, 0, kScreenWidth, kmainScrollViewHeight);
    
    btn.tag = index + 100;
    
    [btn addTarget:self action:@selector(scrollBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return btn;
}



- (void)scrollBtnClick:(UIButton *)button{
    
    if (button.tag == 100) {
        
        
        
        if ([_delegate respondsToSelector:@selector(scrollImageBtnDidClickWithIndex:)] && _delegate){
            
            [_delegate scrollImageBtnDidClickWithIndex:2];
            
            
        }
        
    }else{
        
        
        if ([_delegate respondsToSelector:@selector(scrollImageBtnDidClickWithIndex:)] && _delegate){
            
            [_delegate scrollImageBtnDidClickWithIndex:button.tag - 101];
            
        }
        
    }
    
    
    
    
}


@end
